package com.uenpay.im.adapter;

import android.app.Activity;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.qiyunxin.android.http.adapter.QiYunXinAdapter;
import com.qyx.android.entity.GroupMemberEntity;
import com.qyx.android.message.type.PacketType;
import com.uenpay.im.IMApplication;
import com.uenpay.im.R;
import com.uenpay.im.configuration.APIConfiguration;
import com.uenpay.im.weight.RoundImageView;

import java.util.ArrayList;
import java.util.List;

/**
 * @author SL
 * @copyRight http://www.qiyunxin.com
 *
 */
public class ParticipantAdapter extends QiYunXinAdapter {
	private LayoutInflater inflate;
	public List<GroupMemberEntity> participants = new ArrayList<GroupMemberEntity>();
	private boolean _isCreator;
	private int _joinStatus;

	public ParticipantAdapter(Activity activity,
			List<GroupMemberEntity> data, boolean isCreator, int joinStatus) {
		inflate = LayoutInflater.from(activity);
		_isCreator = isCreator;
		_joinStatus = joinStatus;
		participants = data;
		addDel();
	}

	public int setData(List<GroupMemberEntity> data) {
		participants = data;
		addDel();
		notifyDataSetChanged();
		return participants.size();
	}

	public void addData(List<GroupMemberEntity> data) {
		participants.addAll(data);
		notifyDataSetChanged();
	}

	private void addDel() {
		if (_isCreator && _joinStatus == 1) {
			GroupMemberEntity participant = new GroupMemberEntity();
			participants.add(participant);
		}

		if (_joinStatus == 1) {
			GroupMemberEntity participant = new GroupMemberEntity();
			participants.add(participant);
		}
	}

	@Override
	public int getCount() {
		return participants.size();
	}

	@Override
	public GroupMemberEntity getItem(int i) {
		return participants.get(i);
	}

	@Override
	public long getItemId(int i) {
		return i;
	}

	@Override
	public View getView(int i, View view, ViewGroup viewGroup) {
		final TalkDetailViewHolder holder;
		if (view == null) {
			holder = new TalkDetailViewHolder();
			view = inflate.inflate(R.layout.friend_info_for_talk_detail, viewGroup,false);
			holder.avatar = (RoundImageView) view.findViewById(R.id.avatar);
			holder.name = (TextView) view.findViewById(R.id.name);
			holder.icon_creator_iv = (ImageView) view.findViewById(R.id.icon_creator_iv);
			view.setTag(holder);
		} else {
			holder = (TalkDetailViewHolder) view.getTag();
		}

		GroupMemberEntity participantEntity = participants.get(i);
		holder.name.setText("");
		holder.avatar.SetSrc(R.drawable.default_avatar);
		if (participantEntity.cust_id == 0) {
			if (_isCreator) {
				if (i == participants.size() - 1) {
					holder.avatar.SetSrc(R.drawable.icon_delete);
				} else if (i == participants.size() - 2) {
					holder.avatar.SetSrc(R.drawable.icon_add);
				} else if (i == participants.size() - 1) {
					holder.avatar.SetSrc(R.drawable.icon_add);
				}
			} else {
				if (i == participants.size() - 1) {
					holder.avatar.SetSrc(R.drawable.icon_add);
				}
			}
		} else {
			// if (holder.is_load) {
			holder.avatar.SetUrl(APIConfiguration.getAvatarUrlNormal(
					participantEntity.cust_id + "", PacketType.PERSONAL));
			String name = IMApplication.config
					.getFriendsRemarkName(participantEntity.cust_id + "");
			if (TextUtils.isEmpty(name)) {
				holder.name.setText(participantEntity.nick_name);
			} else {
				holder.name.setText(name);
			}

			// }
		}

		if(i == 0){
			holder.icon_creator_iv.setVisibility(View.VISIBLE);
		}else{
			holder.icon_creator_iv.setVisibility(View.GONE);
		}
		holder.avatar.setVisibility(View.VISIBLE);
		return view;
	}

	private class TalkDetailViewHolder {
		public RoundImageView avatar;
		public TextView name;
		private ImageView icon_creator_iv;
	}
}
